<?php
namespace app\common\model\buss;
use app\common\model\user\User;
use app\api\controller\Pub;
use think\Model;
class Buss extends Model
{
     // 设置当前模型对应的完整数据表名称
        protected $table = 'bussinessinfo';
        protected $pk = 'UserId';
        
        const STATEINT = 1;
        const STATEOK  = 2;
        const STATERETURN = 3;
        const STATEDELETE = 4;


    public function userinfo(){
         return $this->hasOne('app\common\model\user\User','UserId','UserId');
    }

    /**
     * 身份证字段默认隐藏
     * @param  [type] $value [description]
     * @return [type]        [description]
     */
     public static function getCardNumberAttr($value)
    {   
        
        return substr($value, 0 , 4).'****'.substr($value, -4);
    }

    /**
     * 获取状态
     * @param  string $id [description]
     * @return [type]     [description]
     */
    public static function getStatusText($id='')
    {   
        
        $status = [Buss::STATEINT=>'待审核',Buss::STATEOK=>'已通过',Buss::STATERETURN=>'未通过',Buss::STATEDELETE=>'已删除','101'=>'账号正常','102'=>'账号已锁定','103'=>'账号已删除'];
        if($id!==''){
            return $status[$id];
        }else{
            return $status;
        }
    }

    /**
     * 新增角色的时候 初始化用户信息
     * @param  [type] $id     [description]
     * @param  [type] $name   [description]
     * @param  [type] $iphone [description]
     * @return [type]         [description]
     */
    public static function create_user_init($id,$name,$iphone){
        $buss = new Buss();
        $buss->UserId = $id;
        $buss->UserName = $name;
        $buss->UserTel = $iphone;
        $buss->State = Buss::STATEINT;
        $buss->UpTime = date('Y-m-d H:i',time());
        $buss->save();
        return $buss;
    }

    /**
     * 创建收集员信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_create($data){
        $userlogininfo = Pub::getuserid();
        $userid = $userlogininfo['msg'];
        $buss = new Buss();
        $buss->UserId = $userid;
        $buss->UserName = $data['UserName'];
        $buss->CardNumber = $data['CardNumber'];
        $buss->CarNumber = $data['CarNumber'];
        $buss->UserTel = $data['UserTel'];
        $buss->UserImg = $data['UserImg'];
        $buss->CarImg = $data['CarImg'];
        $buss->State = Buss::STATEINT;
        $buss->UpTime = date('Y-m-d H:i',time());
        $buss->save();
        return $buss;
    }
 
    /**
     * 获取信息
     * id  userid
     * isdata 是否返回数组格式
     */
    public static function user_getinfo($id='',$isdata=false,$hidden=['SettingTime','SettingUserId']){
        if(!empty($id)){
            $data = Buss::where('UserId',$id);
        }else{
            $userlogininfo = Pub::getuserid();
            $userid = $userlogininfo['msg'];
            $id = $userid;
            $data = Buss::where('UserId',$userid);
        }
        $data = $data->find();
        if(empty($data)){
            $sb = User::get($id);
            if(!$sb){
                return moerr('缺少用户账号');
            }
            if($sb['Role']==107){
                $data['UserName'] = $sb->Name.'(收集员)';
            }else{
                $data['UserName'] = $sb->Name.'(未备案)';
            }
            $data['UserTel'] = $sb->Iphone;  
            return $data;
        }
        $data->hidden($hidden);
        if($isdata){
            $CardNumber = $data->getData('CardNumber');
            $data = $data->toArray();
            $data['CardNumber'] = $CardNumber;
        }
        return $data;
    }

    /**
     * 用户修改企业信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_reinfo($data){
        $odata = Buss::get(Pub::userid());
        if(empty($odata)){
            return self::user_create($data);
        }
        $file = ['UserName','CardNumber','CarNumber','UserTel','UserImg','CarImg'];
        $nreinfo = 0;
        foreach ($data as $key => $value) {
            $sb =  $key;
            if(!in_array($sb,$file)){
                continue;
            }
            $odata->$sb = $value;
            $nreinfo++;
        }
        if(!empty($nreinfo)){
            $odata->State = Buss::STATEINT;
            $odata->save();
        }
    }

    /**
     * 用户修改企业信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function mana_reinfo($data){
        $odata = self::get($data['id']);
        $userlogininfo = Pub::getuserid();
        $userid = $userlogininfo['msg'];
        $file = ['State','Address'];
        $nreinfo = 0;
        foreach ($data as $key => $value) {
            $sb =  $key;
            if(!in_array($sb,$file)){
                continue;
            }
            $odata->$sb = $value;
            $nreinfo++;
        }
        if(!empty($nreinfo)){
            $odata->SettingUserId =  $userid ;
            $odata->SettingTime = date('Y-m-d H:i',time());
            $odata->save();
        }
        return sok($odata);
    }

    /**
     * 获取目前可用收集员
     * @return [type] [description]
     */
    public static function getoklist($visible=[]){
        $userid = User::powercode_getuserid(201001);
        $rdata = Buss::where('State','<>',Buss::STATEDELETE);
        $rdata->where('UserId','in',$userid);
        $rdata->hidden(['SettingTime','SettingUserId']);
        if(empty(!$visible)){
            $rdata->visible($visible);
        }
        return $rdata->select();
    }

    /**
     * 管理列表
     * @return [type] [description]
     */
    public static function mgergetlist($data,$state = -1){
       if($state==-1){
            $rdata = Buss::where('State','<>',Buss::STATEDELETE);
       }else{
            $rdata = Buss::where('State',$state);
       }
        if(!empty($data['where'])){
            $rdata = $data ->where($data['where']);
        }
        
        $data['pageindex'] = empty($data['pageindex'])?1:$data['pageindex'];
        $count = ceil($rdata->count()/PUBLICSELECTLIMIT);
        $rdata = $rdata->limit(PUBLICSELECTLIMIT)->page($data['pageindex']);

        $rdata = $rdata->with('userinfo');
        $rdata = $rdata->select();
        $rdata->visible(['UserId','UpTime','State','UserName','CarNumber','UserTel','UserImg','CarImg','SettingTime','SettingUserId','Address','userinfo.Name','userinfo.Iphone','userinfo.State']);
        $ndata['list'] = $rdata->toArray();
        $ndata['statetext'] = Buss::getStatusText();
        $ndata['pageinfo'] = ['pageindex'=>$data['pageindex'],'pagemax'=>$count];
        return $ndata;
    }

    /**
     * 获取列表名称
     * @param  [type] $list [description]
     * @return [type]       [description]
     */
    public static function getlistname($list){
        $data =  Buss::where('UserId','in',$list)->field('UserId,UserName')->select();
        $rdata = [];
        foreach ($data as $key => $value) {
            $rdata[$value['UserId']] = $value['UserName'];
        }
        return $rdata;
    } 
}
